<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">

            <el-form-item :label="$t('user.user.nike')" prop="userNike">
                <el-input v-model="queryParams.userNike" :placeholder="$t('user.user.placeholder.nike')" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item :label="$t('user.user.phone')" prop="userPhone">
                <el-input v-model="queryParams.userPhone" :placeholder="$t('user.user.placeholder.phone')" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item :label="$t('user.user.status')" prop="status">
              <el-select v-model="queryParams.status" :placeholder="$t('user.user.placeholder.status')" clearable style="width: 240px">
                <el-option v-for="dict in dict.type.sys_normal_disable" :key="dict.value" :label="dict.label"
                           :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{
                    $t('common.button.query') }}</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('common.button.reset')
                }}</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['app:user:export']">{{ $t('common.button.export') }}</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" border :data="userList" @selection-change="handleSelectionChange">
            <el-table-column label="id" align="center" prop="id" />
            <el-table-column :label="$t('user.user.nike')" align="center" prop="userNike" />
            <el-table-column :label="$t('user.user.sex')" align="center" prop="userSex">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.app_user_sex" :value="scope.row.userSex" />
                </template>
            </el-table-column>
            <el-table-column :label="$t('user.user.phone')" align="center" prop="userPhone" />
            <el-table-column :label="$t('user.user.email')" align="center" prop="userEmail" />
            <el-table-column :label="$t('user.user.country')" align="center" prop="userCountry" />
            <el-table-column :label="$t('user.user.age')" align="center" prop="userAge" />
            <el-table-column :label="$t('user.user.time')" align="center" prop="createTime" />
            <el-table-column :label="$t('user.user.vehicle')" align="center" prop="userVehicleNumber">
                <template slot-scope="scope">
                    <el-button class="custom-button" text="text" @click="getUserVehicle(scope.row)">
                        {{ scope.row.userVehicleNumber }}
                    </el-button>
                </template>
            </el-table-column>
            <el-table-column :label="$t('user.user.emergency')" align="center" prop="userEmergencyNumber">
                <template slot-scope="scope">
                    <el-button class="custom-button" text="text" @click="getUserEmergency(scope.row)">
                        {{ scope.row.userEmergencyNumber }}
                    </el-button>
                </template>
            </el-table-column>
            <el-table-column :label="$t('user.user.status')" align="center" prop="status">
                <template slot-scope="scope">
                    <!-- <dict-tag :options="dict.type.user_account_status" :value="scope.row.status" /> -->
                    <el-switch v-model="scope.row.status" :active-value="0" :inactive-value="1"
                        @change="handleStatusChange(scope.row)"></el-switch>
                </template>
            </el-table-column>
            <el-table-column :label="$t('user.user.createby')" align="center" prop="createBy" />
            <el-table-column :label="$t('common.table.operate')" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-warning" @click="handleReset(scope.row)"
                        v-hasPermi="['app:user:reset']">{{ $t('user.user.reset') }}</el-button>
                    <!-- <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['app:user:edit']">修改</el-button> -->
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['app:user:remove']">{{ $t('common.button.remove') }}</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" />

        <!-- 用户车辆信息 -->
        <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
            <el-table v-loading="vehicleLoading" border :data="vehicleList">
                <el-table-column :label="$t('vehicle.vehicle.vehicleVin')" align="center" prop="vehicleVin" />
                <el-table-column :label="$t('vehicle.vehicle.licenseNumber')" align="center" prop="userNike" />
                <el-table-column :label="$t('vehicle.vehicle.vehicleNike')" align="center" prop="vehicleNick" />
                <el-table-column label="IMEI" align="center" prop="deviceCode" />
                <el-table-column :label="$t('vehicle.type.vehicleModel')" align="center" prop="userNike" />
                <el-table-column :label="$t('vehicle.imgpairing.standard')" align="center" prop="vehicleColor" />
                <el-table-column :label="$t('vehicle.imgpairing.PictureDisplay')" align="center" prop="vehicleImage">
                    <template slot-scope="scope">
                        <image-preview :src="scope.row.vehicleImage" :width="200" :height="150" />
                    </template>
                </el-table-column>
                <!-- <el-table-column label="ICCID" align="center" prop="userNike" /> -->
            </el-table>
        </el-dialog>

        <!-- 用户紧急联系人信息 -->
        <el-dialog :title="emergencyTitle" :visible.sync="emergencyOpen" width="1000px" append-to-body>
            <el-table v-loading="emergencyLoading" border :data="emergencyList">
                <el-table-column :label="$t('user.user.name')" align="center" prop="name" />
                <el-table-column :label="$t('user.user.phone')" align="center" prop="phone" />
            </el-table>
        </el-dialog>
    </div>
</template>

<script>
import { listUser, getUser, reset, getUserVehicleList, getUserEmergencyList, delUser, changeAppUserStatus } from "@/api/appUser/data";

export default {
    name: "User",
    dicts: ['app_user_sex', 'user_account_status', 'sys_normal_disable'],
    data() {
        return {
            emergencyLoading: false,
            emergencyList: [],
            emergencyOpen: false,
            vehicleList: [],
            emergencyTitle: undefined,
            vehicleLoading: false,
            // 按钮loading
            buttonLoading: false,
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // App用户表格数据
            userList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                userCode: undefined,
                userNike: undefined,
                userSex: undefined,
                userPhone: undefined,
                userPassword: undefined,
                userEmail: undefined,
                userProfile: undefined,
                userCountry: undefined,
                createBy:undefined,
                userBirthday: undefined,
                status: undefined,
                imgPath: undefined,
                token: undefined,
                vehicleCode: undefined,
                qqOpenid: undefined,
                iphoneId: undefined,
                wxOpenid: undefined,
                deviceToken: undefined,
                appType: undefined,
                invitationCode: undefined,
                languageIden: undefined,
                mapType: undefined
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {}
        };
    },
    created() {
        this.getList();
    },
    methods: {
        /** 查询App用户列表 */
        getList() {
            this.loading = true;
            listUser(this.queryParams).then(response => {
                this.userList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: undefined,
                userCode: undefined,
                userNike: undefined,
                userSex: undefined,
                userPhone: undefined,
                userPassword: undefined,
                userEmail: undefined,
                userProfile: undefined,
                userCountry: undefined,
                createBy:undefined,
                userBirthday: undefined,
                status: undefined,
                imgPath: undefined,
                token: undefined,
                vehicleCode: undefined,
                qqOpenid: undefined,
                iphoneId: undefined,
                wxOpenid: undefined,
                deviceToken: undefined,
                appType: undefined,
                invitationCode: undefined,
                createTime: undefined,
                updateBy: undefined,
                updateTime: undefined,
                languageIden: undefined,
                mapType: undefined
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = this.$t('common.button.add');
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.loading = true;
            this.reset();
            const id = row.id || this.ids
            getUser(id).then(response => {
                this.loading = false;
                this.form = response.data;
                this.open = true;
                this.title = this.$t('common.button.edit');
            });
        },
        /** 提交按钮 */
        // submitForm() {
        //     this.$refs["form"].validate(valid => {
        //         if (valid) {
        //             this.buttonLoading = true;
        //             if (this.form.id != null) {
        //                 updateUser(this.form).then(response => {
        //                     this.$modal.msgSuccess();
        //                     this.open = false;
        //                     this.getList();
        //                 }).finally(() => {
        //                     this.buttonLoading = false;
        //                 });
        //             } else {
        //                 addUser(this.form).then(response => {
        //                     this.$modal.msgSuccess();
        //                     this.open = false;
        //                     this.getList();
        //                 }).finally(() => {
        //                     this.buttonLoading = false;
        //                 });
        //             }
        //         }
        //     });
        // },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm(this.$t('user.user.placeholder.delete') + ids + this.$t('vehicle.brand.title.query')).then(() => {
                this.loading = true;
                return delUser(ids);
            }).then(() => {
                this.loading = false;
                this.getList();
                this.$modal.msgSuccess("success");
            }).catch(() => {
            }).finally(() => {
                this.loading = false;
            });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('app/user/export', {
                ...this.queryParams
            }, `user_${new Date().getTime()}.xlsx`)
        },

        // 用户状态修改
        handleStatusChange(row) {
            let text = row.status === 1 ? this.$t('user.user.stop') : this.$t('user.user.enable');
            this.$modal.confirm(this.$t('user.user.placeholder.sure') + text + '""' + row.userNike + this.$t('user.user.placeholder.query')).then(function () {
                return changeAppUserStatus(row.id, row.status);
            }).then(() => {
                this.$modal.msgSuccess(text + "成功");
            }).catch(function () {
                row.status = row.status === 0 ? 1 : 0;
            });
        },

        /**
         * 用户账户密码重置
         * @param {*} row
         */
        handleReset(row) {
            this.$modal.confirm(this.$t('user.user.placeholder.sure') +' [ ' + row.userNike + ' ] ' + this.$t('user.user.placeholder.reset')  + this.$t('user.user.placeholder.remark'))
              .then(function () {
                return reset(row.id);
            }).then(() => {
                this.$modal.msgSuccess("user.user.resetSuccess");
            }).catch(function () {
                this.$modal.msgError("user.user.resetError");
            });
        },

        /**
         * 获取当前用户所有的车辆信息
         */
        getUserVehicle(row) {
            this.title = this.$t('user.user.vehicle');
            this.open = true;
            this.vehicleLoading = true;
            var data = {};
            data.userCode = row.userCode;
            getUserVehicleList(data).then(response => {
                this.vehicleList = response.data;
                this.vehicleLoading = false;
            })
        },

        /**
         * 获取当前用户所有紧急联系人信息
         * @param {*} row
         */
        getUserEmergency(row) {
            this.emergencyTitle = this.$t('user.user.vehicle');
            this.emergencyOpen = true;
            this.emergencyLoading = true;
            var data = {};
            data.userId = row.id;
            getUserEmergencyList(data).then(response => {
                this.emergencyList = response.data;
                this.emergencyLoading = false;
            })
        }
    }
};
</script>
<style >
.custom-button {
    border-color: transparent;
    color: blue;
}
</style>
